#include "thread.hpp"

namespace mina
{
	namespace thread
	{
		IMPL_LOGGER(Thread, logger);

		Thread::Thread(const char * name)
			: _start_flag(false),
			_name(name),
			_id(0)
		{
		}

		Thread::~Thread()
		{
			int ret = 0;
			if (_start_flag && (ret = ::pthread_join(_id, 0)) != 0)
			{
				LOG4CPLUS_ERROR(logger, "pthread_join. errno: " << ret);
			}
		}

		int Thread::start()
		{
			if (_start_flag)
			{
				return EINVAL;
			}

			_start_flag = true;

			int ret = 0;
			if ((ret = ::pthread_create(&_id, 0, &run, this)) != 0)
			{
				LOG4CPLUS_ERROR(logger, "pthread_create error. errno: " << ret);
			}
			return ret;
		}
	}
}

